﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DonetArxLib.CuveProjection
{
    internal static class ExampleFunc
    {


        public static void CreatePolyline2d()
        {
            // Get the current document and database, and start a transaction
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Open the Block table record for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                // Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                      OpenMode.ForWrite) as BlockTableRecord;
                // Create a 2D polyline with two segments (3 points)
                Polyline2d acPoly2d = new Polyline2d();
                acPoly2d.SetDatabaseDefaults();
                // Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acPoly2d);
                acTrans.AddNewlyCreatedDBObject(acPoly2d, true);
                // Before adding vertexes, the polyline must be in the drawing
                Point3dCollection acPts2dPoly = new Point3dCollection();
                acPts2dPoly.Add(new Point3d(1, 1, 0));
                acPts2dPoly.Add(new Point3d(1, 2, 0));
                acPts2dPoly.Add(new Point3d(2, 2, 0));
                acPts2dPoly.Add(new Point3d(3, 2, 0));
                acPts2dPoly.Add(new Point3d(4, 4, 0));
                foreach (Point3d acPt3d in acPts2dPoly)
                {
                    Vertex2d acVer2d = new Vertex2d(acPt3d, 0, 0, 0, 0);
                    acPoly2d.AppendVertex(acVer2d);
                    acTrans.AddNewlyCreatedDBObject(acVer2d, true);
                }
                // Set the normal of the 2D polyline
                acPoly2d.Normal = new Vector3d(0, 1, 2);
                acPoly2d.Elevation = 10;
                // Get the first coordinate of the 2D polyline
                Point3dCollection acPts3d = new Point3dCollection();
                Vertex2d acFirstVer = null;
                foreach (ObjectId acObjIdVert in acPoly2d)
                {
                    acFirstVer = acTrans.GetObject(acObjIdVert,
                                                   OpenMode.ForRead) as Vertex2d;
                    acPts3d.Add(acFirstVer.Position);
                    break;
                }
                // Get the first point of the polyline and
                // use the eleveation for the Z value
                Point3d pFirstVer = new Point3d(acFirstVer.Position.X,
                                                      acFirstVer.Position.Y,
                                                      acPoly2d.Elevation);
                // Translate the OCS to WCS
                Matrix3d mWPlane = Matrix3d.WorldToPlane(acPoly2d.Normal);
                Point3d pWCSPt = pFirstVer.TransformBy(mWPlane);
                Application.ShowAlertDialog("The first vertex has the following " +
                                                  "coordinates:" +
                                                  "\nOCS: " + pFirstVer.ToString() +
                                                  "\nWCS: " + pWCSPt.ToString());
                // Save the new objects to the database
                acTrans.Commit();
            }
        }
    }
}
